﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class KhoaHocProvider
    {
        public static List<KhoaHoc> LayDanhSachKhoaHoc()
        {
            var lst = new List<KhoaHoc>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.KhoaHocs select u);

                foreach (var row in query)
                {
                    var kh = new KhoaHoc();
                    kh.MaKH = row.MaKH;
                    kh.MaMH = row.MaMH;
                    kh.TenKH = row.TenKH;
                    kh.ThoiGianBD = row.ThoiGianBD;
                    kh.ThoiGianKT = row.ThoiGianKT;

                    lst.Add(kh);
                }
            }

            return lst;
        }

        public static int ThemKhoaHoc(KhoaHoc kh)
        {
            int maKH = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.KhoaHocs.Add(kh);
                    context.SaveChanges();
                    maKH = kh.MaMH;
                }
            }
            catch
            {

            }
            return maKH;
        }

        public static bool XoaKhoaHoc(int maKH)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    //var kh = context.KhoaHocs.Single(m => m.MaKH.Equals(maKH));

                    //context.KhoaHocs.Remove(kh);
                    //context.SaveChanges();

                    var query = (from u in context.KhoaHocs
                                 where u.MaKH.Equals(maKH)
                                 select u).First();

                    context.KhoaHocs.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool CapNhatKhoaHoc(KhoaHoc kh)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.KhoaHocs
                                 where u.MaKH.Equals(kh.MaKH)
                                 select u).First();
                    query.MaMH = kh.MaMH;
                    query.TenKH = kh.TenKH;
                    query.ThoiGianBD = kh.ThoiGianBD;
                    query.ThoiGianKT = kh.ThoiGianKT;

                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
